package com.example.hilt_mvvm_http.dialog

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.hilt_mvvm_http.R

/**
 * 消息提示对话框
 */
class MessageNoticeDialog(//主构造
    private val title:String? = null,
    private val message: SpannableStringBuilder? = null, //富文本
    private val confirmText :String = "确定",
    private val cancelText:String = "取消",
    private val onConfirmClick :(()->Unit)? = null,
    private val onCancelClick :(()->Unit)? = null,
    private val onlyConfirmButton:Boolean = false,//是否只显示确定
    private val cancelable :Boolean = false, //是否可以取消
    private val alignment:Int = Gravity.CENTER,//内容的居中样式
    private val confirmClickDismiss: Boolean = true //点击确定是否关闭对话框
    ):DialogFragment() {

    companion object{
        val TAG:String = MessageNoticeDialog::class.java.simpleName
    }

        //次构造必须继承主构造
        constructor(
            title: String? = null,
            message: String? = null,//普通字符串
            onConfirmClick: (() -> Unit)? = null,
            onCancelClick: (() -> Unit)? = null,
            onlyConfirmButton: Boolean = false,
            confirmText: String = "确定",
            cancelText: String = "取消",
            cancelable: Boolean = false,
            alignment: Int = Gravity.CENTER,
            confirmClickDismiss: Boolean = true
        ):this(
            title,
            SpannableStringBuilder(message),//富文本
            confirmText,
            cancelText,
            onConfirmClick,
            onCancelClick,
            onlyConfirmButton,
            cancelable,
            alignment,
            confirmClickDismiss
        )

    /**
     * 设置对话框的样式
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MessageNoticeDialog)
    }

    /**
     * 设置窗体属性
     */
    override fun onStart() {
        super.onStart()
        //对话框自己窗体属性
        val window = dialog!!.window
        val wlp = window!!.attributes
        //activity窗体属性
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        //将对话框的窗体属性重新赋值成activity的0.72倍数
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        wlp.gravity = Gravity.CENTER
        wlp.width = (dm.widthPixels * 0.72).toInt()
        window.attributes = wlp



    }

    private lateinit var mParentView:View

    /**
     * 加载布局
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mParentView = inflater.inflate(R.layout.dialog_message_notice,container,false)
        return mParentView
    }

    /**
     * 设置内容
     *  private val title:String? = null,
     *     private val message: SpannableStringBuilder? = null, //富文本
     *     private val confirmText :String = "确定",
     *     private val cancelText:String = "取消",
     *     private val onConfirmClick :(()->Unit)? = null,
     *     private val onCancelClick :(()->Unit)? = null,
     *     private val onlyConfirmButton:Boolean = false,//是否只显示确定
     *     private val cancelable :Boolean = false, //是否可以取消
     *     private val alignment:Int = Gravity.CENTER,//内容的居中样式
     *     private val confirmClickDismiss: Boolean = true //点击确定是否关闭对话框
     */

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(cancelable)
        dialog?.setCanceledOnTouchOutside(cancelable) //点击外部是否可以取消

        //内容
        mParentView.findViewById<TextView>(R.id.tv_message).apply {
            text = message
            gravity = alignment
            highlightColor = Color.TRANSPARENT //富文本点击高亮颜色变为透明
            movementMethod = LinkMovementMethod.getInstance() //富文本点击不起作用，需要去除掉原来的点击
        }
        //标题
        if(title == null){
            mParentView.findViewById<TextView>(R.id.tv_title).visibility  = View.GONE
        }else{
            mParentView.findViewById<TextView>(R.id.tv_title).text = title
        }
        //取消按钮
        if(onlyConfirmButton){//只显示确定
            mParentView.findViewById<TextView>(R.id.btn_cancel).visibility = View.GONE
            mParentView.findViewById<View>(R.id.line).visibility = View.GONE
        }else{
            mParentView.findViewById<TextView>(R.id.btn_cancel).visibility = View.VISIBLE
            mParentView.findViewById<View>(R.id.line).visibility = View.VISIBLE
            mParentView.findViewById<TextView>(R.id.btn_cancel).setOnClickListener {
                onCancelClick?.invoke() //回调
                dismiss()
            }
            if(cancelText != null){
                mParentView.findViewById<TextView>(R.id.btn_cancel).text = cancelText
            }
        }
        //确认按钮
        mParentView.findViewById<TextView>(R.id.btn_confirm).setOnClickListener {
            onConfirmClick?.invoke()
            if(confirmClickDismiss){
                dismiss()
            }
        }
        if(confirmText != null){
            mParentView.findViewById<TextView>(R.id.btn_confirm).text = confirmText
        }
    }

    /**
     * 显示对话框
     */
    fun show(fragmentManager: FragmentManager){
        Log.d(TAG, "show: ")
        if(fragmentManager.findFragmentByTag(TAG)?.isVisible == true){//判断当前对话框是否显示
            return
        }
        this.show(fragmentManager,TAG)
        
    }









}